﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Lamex.Library;
using Lamex.Interface;

namespace Lamex.mEfficiency.UI
{
    public partial class frmEFF_Powder_Coating_Seprate : frmBaseChild
    {
        string strD1, strD2, strCondition;
        string strSql;

        public frmEFF_Powder_Coating_Seprate()
        {
            InitializeComponent();
        }

        private void frmEFF_Powder_Coating_Seprate_Load(object sender, EventArgs e)
        {
            try { base.InitButtons(); } catch { }//初始化标准功能，因要应用于RibbonForm，所以加入异常处理

            string CurUser = Lamex.Common.Loginer.CurrentUser.Account;

            DateTime now = DateTime.Now;
            dteStart.DateTime = now.AddDays(-(int)now.DayOfWeek + 1 - 7);
            dteEnd.DateTime = now.AddDays(7 - (int)now.DayOfWeek - 7);

            gridView1.OptionsBehavior.Editable = false;//只读
            gridView1.Appearance.HeaderPanel.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;//列标题居中
        }

        private void sbtnView_Click(object sender, EventArgs e)
        {
            gridView1.Columns.Clear();

            getQueryDate();//获取起止时间段
            getQueryCondition();//获取起止时间段

            strSql = @"SELECT Scn_Date AS 扫描日期,a.Part_No AS 部件编号,Part_Desc AS 部件描述,Drawing_No AS 图纸编号,
                        b.MRP_CONTROLLER AS MRP,b.PRODUCT_ALLOCATION AS 产品分配,
                        CAST(InsideQty*Part_QTY/Order_QTY AS decimal(18,2)) AS 部件数量,
                        CAST(InsideQty*Part_QTY/Order_QTY*COMB_TIME/60 AS decimal(18,2)) AS 入仓工时
                        FROM View_OP_Inside_Detail AS a
                        LEFT JOIN
                        IE_Part_MasterData AS b
                        ON a.Part_No=b.PART_NO
                        WHERE PROCESS_CODE='P21' AND " + strCondition + @"
                        AND Scn_Date>='" + strD1 + @"' AND  Scn_Date<='" + strD2 + @"'";

            DataTable dt1 = Lamex.Base.BaseBuildSql.GetDataTableBySql(strSql);
            if (dt1.Rows.Count > 0)
            {
                gridControl1.DataSource = dt1;
            }

            TotalSum();//统计求和

            //DevExpress XtraGrid GridColumn 列宽自适应
            //不能设置GridColumn的MaxWidth。
            //gridView1.BestFitColumns();
            //gridView1.HorzScrollVisibility = DevExpress.XtraGrid.Views.Base.ScrollVisibility.Always;
            //gridView1.OptionsView.ColumnAutoWidth = false;

            gridView1.OptionsView.ColumnAutoWidth = true;

            this.Text = "喷粉工序(P21)入仓明细 ( " + strD1 + " - " + strD2 + " )";
        }

        //获取起止时间段
        private void getQueryDate()
        {
            strD1 = dteStart.Text.ToString();

            if (dteEnd.Text == "")
            {
                strD2 = strD1;
            }
            else
            {
                strD2 = dteEnd.Text.ToString();
            }
        }

        //获取查询条件
        private void getQueryCondition()
        {
            if (checkBoxPanel.Checked == true && checkBoxMCG.Checked == true)
            {
                strCondition = "1=1";
            }
            else if (checkBoxPanel.Checked == true)
            {
                strCondition = "MRP_CONTROLLER='P00'";
            }
            else
            {
                strCondition = "MRP_CONTROLLER!='P00'";
            }
        }

        private void rbtnCurWeek_CheckedChanged(object sender, EventArgs e)
        {
            if (rbtnCurWeek.Checked == true)
            {
                doClearViewAndChartData();

                DateTime now = DateTime.Now;
                dteStart.DateTime = now.AddDays(-(int)now.DayOfWeek + 1);
                dteEnd.DateTime = now.AddDays(7 - (int)now.DayOfWeek);
            }
        }

        private void rbtnLastWeek_CheckedChanged(object sender, EventArgs e)
        {
            if (rbtnLastWeek.Checked == true)
            {
                doClearViewAndChartData();

                DateTime now = DateTime.Now;
                dteStart.DateTime = now.AddDays(-(int)now.DayOfWeek + 1 - 7);
                dteEnd.DateTime = now.AddDays(7 - (int)now.DayOfWeek - 7);
            }
        }

        private void dteStart_EditValueChanged(object sender, EventArgs e)
        {
            doClearViewAndChartData();
        }

        private void dteEnd_EditValueChanged(object sender, EventArgs e)
        {
            doClearViewAndChartData();
        }

        private void checkBoxPanel_CheckedChanged(object sender, EventArgs e)
        {
            doClearViewAndChartData();
        }

        private void checkBoxMCG_CheckedChanged(object sender, EventArgs e)
        {
            doClearViewAndChartData();
        }

        //重置图表
        private void doClearViewAndChartData()
        {
            gridView1.Columns.Clear();
            this.Text = "喷粉入仓";
        }

        private void TotalSum()
        {
            gridView1.Columns[5].SummaryItem.SummaryType = DevExpress.Data.SummaryItemType.Count;
            gridView1.Columns[5].SummaryItem.DisplayFormat = "合计：";

            if (gridView1.Columns.Contains(gridView1.Columns["部件数量"]))
            {
                gridView1.Columns["部件数量"].SummaryItem.SummaryType = DevExpress.Data.SummaryItemType.Sum;
                this.gridView1.Columns["部件数量"].SummaryItem.DisplayFormat = "{0:N}";
                // this.gridView1.Columns["部件数量"].Width = 80;
            }

            if (gridView1.Columns.Contains(gridView1.Columns["入仓工时"]))
            {
                gridView1.Columns["入仓工时"].SummaryItem.SummaryType = DevExpress.Data.SummaryItemType.Sum;
                this.gridView1.Columns["入仓工时"].SummaryItem.DisplayFormat = "{0:N}";
                //this.gridView1.Columns["工时"].Width = 80;
            }
        }

        #region 实例化标准功能

        private void 隐藏选项区ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            panelTop.Height = 0;
        }

        private void 显示选项区ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            panelTop.Height = 60;
        }

        private void 导出ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Lamex.Common.ExecelHelper.GridViewToExcel(gridView1);
        }

        private void 打印ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Lamex.Common.ExecelHelper.GridViewToPrint(gridControl1, this.Text);
        }

        public override void DoView(IButtonInfo sender)
        {
            sbtnView_Click(null, null);
            base.DoView(sender);
        }

        public override void DoExport(IButtonInfo sender)
        {
            导出ToolStripMenuItem_Click(null, null);
            base.DoExport(sender);
        }

        public override void DoPrint(IButtonInfo sender)
        {
            打印ToolStripMenuItem_Click(null, null);
            base.DoPrint(sender);
        }

        #endregion

    }
}